using System;
using System.Collections.Generic;
using System.Text;

using System.Security.Principal;

namespace Mitosoft.Definitions.Application {

  public class AppContext : IAppContext {

    public AppContext() { }

    protected IAppContext parent;

    public IAppContext Parent {
      get { return this.parent; }
      protected set { this.parent = value; }
    }

    private IPrincipal user;

    public IPrincipal User {
      get { return this.user; }
      protected set { this.user = value; }
    }

    public static IAppContext InitializeNew() {
      return InitializeNew<AppContext>();
    }

    protected static ContextType InitializeNew<ContextType>() where ContextType : AppContext, new() {
      return new ContextType();
    }

    public static IAppContext InitializeForUser(IPrincipal user) {
      return InitializeForUser(null, user);
    }

    public static IAppContext InitializeForUser(IAppContext parent, IPrincipal user) {
      return InitializeForUser<AppContext>(parent, user);
    }

    protected static ContextType InitializeForUser<ContextType>(IAppContext parent, IPrincipal user) where ContextType : AppContext, new() {
      ContextType context = new ContextType();
      context.Parent = (parent != null ? parent : InitializeNew<ContextType>());
      context.User = user;
      return context;
    }

  }
}
